<?php

/**
 * Created by Sven.
 * Date: 2016/12/30
 * Time: 14:24
 * 步骤:
 *  检查各种指标
 *      错误类型   文件类型
 *  检查并创建路径
 *
 *  获取硬路径的完整路径、生成相对uploads的的地址
 *
 *  开始上传
 *      判断$_FILES 里的 tmp_name 是否是可上传文件
 *      执行move_upload_file 上传文件并返回节骨
 *
 *  提供获取最终保存路径的方法 getLinkPath
 *          这个路径是保存在数据库中的，
 *         这个路径将会在img连接中显示
 */
class FileUpload {
    private $error;
    private $type;
    private $maxsize ;

    private $path; // 主目录
    private $today; // 日期：20161231
    private $name; // 文件名
    private $ext;
    private $tempName; // 临时文件名

    private $linkPath; // 用于保存数据库的，显示在连接中的路径

    private $allowTypes = array('image/jpeg', 'image/png' , 'image/pjpeg', 'image/x-png', 'image/gif');

    public function __construct($_file, $_maxsize) {
        $this->maxsize = $_maxsize;

        $this->error = $_FILES[$_file]['error'];
        $this->checkError();
        $this->type = $_FILES[$_file]['type'];
        $this->checkType();
        $this->path = ROOT_PATH.'/'.UPLOAD_DIR;
        $this->today = date('Ymd');
        $this->checkPath();
        $this->name = $_FILES[$_file]['name'];
        $this->setExt();
        $this->tempName = $_FILES[$_file]['tmp_name'];

    }

    // 检查是否出现错误
    private function checkError(){
        if (!empty($this->error)){
            switch ($this->error){
                case 1:
                    Tool::alertBack('警告：上传文件大小超出了约定值');
                    break;
                case 2:
                    Tool::alertBack('警告：上传文件超出了HTML表单约定值');
                    break;
                case 3:
                    Tool::alertBack('警告：文件只有部分被上传');
                    break;
                case 4:
                    Tool::alertBack('警告：没有任何文件被上传');
                    break;

            }
        }
    }

    // 检查类型是否错误
    private function checkType(){
        if (!in_array($this->type,  $this->allowTypes)){
            Tool::alertBack('警告：上传文件的格式不符合');
        }
    }

    // 检查上传目录
    private function checkPath(){
        if (!is_dir($this->path) || !is_writable($this->path)){
            if (!mkdir($this->path)){
                Tool::alertBack('警告：创建主目录失败');
            }
        }

        if (!is_dir($this->path.$this->today) || !is_writable($this->path.$this->today)){
            if (!mkdir($this->path.$this->today)){
                Tool::alertBack('警告：创建子目录失败');
            }
        }

    }

    // 设置后缀名
    private function setExt(){
        $_name_array = explode('.',  $this->name);
        $this->ext = $_name_array[count($_name_array) - 1];
    }

    // 获取新上传的文件名的硬路径
    private function getFilename(){
        $_temp = sha1($this->name).mt_rand(100,1000).'.'.$this->ext;
        $this->linkPath = UPLOAD_DIR.$this->today.'/'.$_temp;
        return $this->path.$this->today.'/'.$_temp;
    }

    public function upload(){
        if (is_uploaded_file($this->tempName)){
            if (move_uploaded_file( $this->tempName,  $this->getFilename() )){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function getLinkPath(){
        return $this->linkPath;
    }

}